package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.ProductionOrder;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.Task;
import com.qidianit.mes.pro.mapper.ProductionOrderMapper;
import com.qidianit.mes.pro.mapper.TaskMapper;
import com.qidianit.mes.pro.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ Description:
 * @ Author:WuJingLei
 * @ Date: 2025/9/24 -15:54
 */
@Service
public class TaskServiceImpl implements TaskService {
    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private ProductionOrderMapper productionOrderMapper;
    @Override
    public Result addTask(Task task) {
        //编码不重复
        if (taskMapper.queryByTaskCode(task.getTaskCode())!=null){
            return Result.error("任务编码已存在");
        }
        //排产数量是否超出工单数量
        //排产数量
        int numSum = 0;
        //工单数量
        ProductionOrder productionOrder = productionOrderMapper.selectById(task.getProductionOrderId());
        //所有的任务
        List<Task> tasks = taskMapper.queryList(task);
        //排产数量相加
        for (Task task1 : tasks) {
            numSum += task1.getScheduleQuantity();
        }
        if (numSum+task.getScheduleQuantity()>productionOrder.getProductionOrderQuantity()){
            int num= (int) (productionOrder.getProductionOrderQuantity()-numSum);
            return Result.error("排产数量超过了工单数量，现在还没有排产的工单数量为:"+num);
        }
        //完成时间的计算
        if (task.getStartProductionTime()!=null && task.getProductionDuration()!=null){
            task.setEstimatedCompletionTime(task.getStartProductionTime().plusMinutes(task.getProductionDuration()));
        }

        return taskMapper.addTask(task)>0?Result.success(task):Result.error("操作失败");
    }

    @Override
    public Result<List<Task>> queryTaskList(Task task) {
        if (task.getProcessId()==null ||task.getProductionOrderId() ==null||task.getWorkflowId() ==null){
            return Result.error("任务列表加载失败");
        }
        List<Task> tasks = taskMapper.queryList(task);
        return Result.success(tasks);
    }

    @Override
    public Result getByTaskCode(String code) {
        return Result.success(taskMapper.queryByTaskCode(code));
    }

    @Override
    public Result updateTask(Task task) {
        //编码不重复
        Task task1 = taskMapper.queryByTaskCode(task.getTaskCode());
        if (task1!=null&&!task1.getTaskId().equals(task.getTaskId())){
            return Result.error("任务编码已存在");
        }
        //排产数量是否超出工单数量
        //排产数量
        int numSum = 0;
        //工单数量
        ProductionOrder productionOrder = productionOrderMapper.selectById(task.getProductionOrderId());
        //所有的任务
        List<Task> tasks = taskMapper.queryList(task);
        //排产数量相加
        for (Task task2 : tasks) {
            numSum += task2.getScheduleQuantity();
        }
        if (numSum+task.getScheduleQuantity()>productionOrder.getProductionOrderQuantity()){
            int num= (int) (productionOrder.getProductionOrderQuantity()-numSum);
            return Result.error("排产数量超过了工单数量，现在还没有排产的工单数量为:"+num);
        }
        //完成时间的计算
        if (task.getStartProductionTime()!=null && task.getProductionDuration()!=null){
            task.setEstimatedCompletionTime(task.getStartProductionTime().plusMinutes(task.getProductionDuration()));
        }
        return taskMapper.updateTask(task)>0?Result.success():Result.error("任务更新失败");
    }

    @Override
    public Result deleteTask(Long id) {
        return taskMapper.deleteByTaskId(id)>0?Result.success():Result.error("删除任务操作失败");
    }

    @Override
    public Result batchDelete(Long[] ids) {
        return taskMapper.batchDeleteByTaskIds(ids)>0?Result.success():Result.error("批量删除操作失败");
    }

    @Override
    public Result<PageInfo<Task>> queryPage(Task task, int pageNum, int size) {
        //分页
        PageHelper.startPage(pageNum,size);
        List<Task> tasks = taskMapper.queryList(task);
        return Result.success(new PageInfo<>(tasks));

    }
}
